﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace Uranus.ChinaService
{
    /// <summary>
    /// ChinaRet 的摘要说明
    /// </summary>
    public class ChinaRet : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string cmdid = context.Request["CmdId"];
            if (!string.IsNullOrEmpty(cmdid))
            {
                try
                {
                    ChinapnrAPI.BaseChinapnr baseChinapnr = ChinaServiceCommon.WriteLogByContent(context);

                    if (baseChinapnr == null)
                    {
                        context.Response.End();
                        return;
                    }
                    if (baseChinapnr.RespCode == "000" || new List<string> { "cash", "initiativetender", "netsave" }.Contains(cmdid.ToLower()))
                    {
                        Action<HttpContext> a = (sncContext) =>
                        {
                            ChinaServiceCommon.MethodRequest(cmdid, sncContext);
                        };
                        a.BeginInvoke(context, null, null);
                    }
                    string result = ((string.IsNullOrEmpty(baseChinapnr.SecRespDesc) || baseChinapnr.SecRespDesc == "未知错误"
                        || (baseChinapnr.SecRespDesc == baseChinapnr.RespDesc))
                                        ? baseChinapnr.RespDesc
                                        : (baseChinapnr.RespDesc + "-" + baseChinapnr.SecRespDesc));

                    string str2 = ConfigurationManager.AppSettings["ChinaFrontResponseUrl"] + "?RespCode=" +
                         baseChinapnr.RespCode + "&CmdId=" + cmdid + "&UsrCustId=" +
                         context.Request["UsrCustId"] +
                         ((!string.IsNullOrEmpty(baseChinapnr.OrdId))
                             ? "&OrdId=" + baseChinapnr.OrdId
                             : string.Empty) +
                         ((!string.IsNullOrEmpty(baseChinapnr.MerPriv))
                             ? "&MerPriv=" + baseChinapnr.MerPriv
                             : string.Empty) + "&RespDesc=" + result + "";
                    context.Response.Redirect(str2, false);

                    return;
                }
                catch (Exception ex)
                {
                    dynamic d = new System.Dynamic.ExpandoObject();
                    d.StackTrace = ex.StackTrace;
                    ChinaServiceCommon.WriteErrorLog("ChinaReceive1 ProcessRequest", ex.ToString(), null, context.Request, true);
                }
            }
            else
            {
                context.Response.Write(ConfigurationManager.AppSettings["ChinaVersion"]);
                context.Response.End();
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}